<template>
    <div class="h-full flex-col">
        <SearchForm @handle-reset="resetParams" @handle-search="getList">
            <template #header>
                <el-button @click="cretedLink">创建链接</el-button>
            </template>
            <el-form :model="searchForm" inline>
                <el-form-item>
                    <el-input v-model="searchForm._id" clearable @change="getList" class="w-220px" placeholder="链接ID" />
                </el-form-item>
                <el-form-item>
                    <el-select v-model="searchForm.status" placeholder="状态" clearable  @change="getList" class="w-220px">
                        <el-option label="启用" :value="1" />
                        <el-option label="禁用" :value="0" />
                    </el-select>
                </el-form-item>
            </el-form>
        </SearchForm>
        <div class="page-card h-full" v-loading="loading">
            <el-table :data="tableData">
                <el-table-column label="_id" prop="_id" align="center" />
                <el-table-column label="链接" align="center">
                    <template #default="{ row }">
                        <el-text class="mx-1" type="primary">{{ row.link_url }}</el-text>
                        <el-link icon="CopyDocument" v-copy="row.link_url" />
                    </template>
                </el-table-column>
                <el-table-column label="账号" align="center">
                    <template #default="{ row }">
                        {{ row.adminUserId.userName || '/' }}
                    </template>
                </el-table-column>
                <el-table-column label="标识" align="center">
                    <template #default="{ row }">
                        {{ row.identification || '/' }}
                    </template>
                </el-table-column>
                <el-table-column label="点击" prop="clicks" align="center" />
                <el-table-column label="转化用户" prop="placeOrder" align="center" />
                <!-- <el-table-column label="父账号" prop="phone" align="center" /> -->
                <!-- 条件查看 -->
                <!-- <el-table-column label="角色" align="center">
            <template #default="{ row }">
              <el-tag>
                {{ row.role === 1 ? '超级管理员' : '普通用户' }}
              </el-tag>
            </template>
          </el-table-column> -->
                <el-table-column label="状态" align="center">
                    <template #default="{ row }">
                        <el-tag :type="row.status === 1 ? 'success' : 'info'">
                            {{ row.status === 1 ? '正常' : '禁用' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="createTime" align="center" />
                <el-table-column label="操作" width="160" align="center">
                    <template #default="scope">
                        <!-- <el-button type="primary" link @click="onAddOrUpdate(scope.row)">编辑</el-button> -->
                        <el-button type="danger" link @click="onDelete(scope.row._id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination :pageable="page" :handleSizeChange="handleSizeChange" :handleCurrentChange="handleCurrentChange" />
            <el-dialog v-model="createdUserLinkStatus" title="创建链接" width="20%" @closed="clearAddModel">
                <el-form :model="addusertagdata" label-width="60px" @submit.prevent>

                    <el-form-item label="标识：">
                        <el-input v-model="addusertagdata.identification" style="width: 70%;" @keyup.enter="onSubmit"
                            type="text" placeholder="用于标识推广平台y用途等:(抖音，朋友圈...)" />
                    </el-form-item>
                    <!-- <el-form-item>
                        <el-button type="primary" @click="onSubmit">确认</el-button>
                        <el-button>取消</el-button>
                    </el-form-item> -->
                </el-form>
                <template #footer>
                    <div class="dialog-footer">
                        <el-button type="primary" @click="onSubmit">确认</el-button>
                        <el-button @click="clearAddModel">取消</el-button>
                    </div>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useTable } from '@/hooks/useTable';
import { getLinkLIstApi, deleteLinkApi, createLinkApi } from '@/api/promotion';
import { useRouter } from 'vue-router';





const addusertagdata = reactive({
    identification: ''
})
const createdUserLinkStatus = ref(false);
// const addOrUpdateRef = ref();
interface searchFormType {
    status: string | number;
    _id: any;
}
const searchForm = reactive<searchFormType>({
    status: '',
    _id: ''
});
const router = useRouter();

const { id = '' } = router.currentRoute.value.query

searchForm._id = id

const callback = (res: any) => {
    res.list.forEach((item: any) => {

        // item.link_url = import.meta.env.VITE_GLOB_API_URL + 'link/binduser/' + item._id
        item.link_url = 'http://doulingyang.cn/dlyadminapi/link/binduser/' + item._id
    })
    return res;
}
const { tableData, page, loading, resetParams, getList, handleCurrentChange, handleSizeChange } =
    useTable({
        apiFn: getLinkLIstApi,
        params: searchForm,
        callback
    });

const onDelete = (id: string) => {
    ElMessageBox.confirm('删除后该连接将被释放，不再关联到您的账号！', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(() => {
            deleteLinkApi(id).then(res => {
                ElMessage({
                    type: 'success',
                    message: '删除成功'
                });
                getList();
            })

        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消操作'
            });
        });
};
const cretedLink = () => {
    createdUserLinkStatus.value = true;
    // alert('创建链接')
    // console.log('cretedLink');

}
const onSubmit = () => {
    createLinkApi(addusertagdata).then(res => {
        console.log('cretedLink', res);
        ElMessage({
            type: 'success',
            message: '创建成功'
        });
        clearAddModel()
        getList();
    })
}
const clearAddModel = () => {
    createdUserLinkStatus.value = false;
    addusertagdata.identification = ''
}
</script>

<style lang="scss" scoped></style>
